package com.boxin.ims.modules.wechat.busi;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.wechat.entity.BusinessNewsReply;
import com.boxin.ims.modules.wechat.entity.NewsReply;
import com.boxin.ims.modules.wechat.entity.UserBusiness;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.BusinessNewsReplyService;
import com.boxin.ims.modules.wechat.service.UserDefineService;
import com.boxin.ims.modules.wechat.utils.DataUtils;
import com.boxin.ims.modules.wechat.utils.ScoreUtils;
import com.boxin.ims.modules.wechat.utils.WechatUtils;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;


/**
 * @author Jakemanse
 * @date 2013-9-23
 * @function <p>签到处理器 </p>
 */
@Service
public class SignProcessor extends BusinessProcessor {
	
	@Autowired
	UserDefineService userDefineService;
	
	@Autowired
	BusinessNewsReplyService businessNewsReplyService;

	public SignProcessor() {
		super();
	}
	
	
	@Override
	public ProcessMessage process() {
		WechatMember member =	memberCheckAndReply();
		if(member != null){
			String reply  = "";
			String upcon = textMessage.getContent();
			
			
			UserBusiness userBusiness = 	WechatUtils.getUserBusinessByKeyWords(user.getId(),upcon);
			
			UserDefine userDefine = WechatUtils.getUserDefineByUserId(weChat.getUser().getId());	//定义
			if(userDefine != null && UserDefine.NO.equals(userDefine.getSignSwitch())){	//签到服务停止
				reply =  "签到积分活动已停止,启动时间请等通知,谢谢!" ;
				if(StringUtils.isNotEmpty(userDefine.getSignSwitchDesc())){
					reply = userDefine.getSignSwitchDesc();
				}
				writeToUser(reply);
				return null;
			}
			
			if(DateUtils.getTodayString().equals(member.getLastSignTime())){	//今天已签到过了
				Integer score = member.getScore();
				score = (score==null) ? 0 :score;
				reply = WechatUtils.processUserBusinessReply(user.getId(), upcon, score);
				if(com.thinkgem.jeesite.common.utils.StringUtils.isEmpty(reply)){
					reply = "今天您已签到过了,您当前的积分为:"+score+"分!";
				}else{
					reply =  "今天您已签到过了," + reply;
				}
				
				
				
				List<BusinessNewsReply>	bnrList = businessNewsReplyService.findByTypeSourceId("business", user.getId() , userBusiness.getId()+"");
				
				/**
				 * 多图文回复
				 */
				if(bnrList != null  && !bnrList.isEmpty() ){
					
					List<NewsReply> newsReplies = DataUtils.convertToNewsReplyList(bnrList);
					if(newsReplies!=null  && !newsReplies.isEmpty()){
						for(NewsReply newsReply : newsReplies){
							String desc = newsReply.getDescription();
							desc = desc.replaceAll("%分值%", userDefine.getSignScore()+"");
							desc = desc.replaceAll("%积分%", score+"");
							newsReply.setDescription(desc);
						}
						
						
						this.writeMultiImagesToUser(newsReplies);
						return null;
					}
					
				}
				
			}else{//今天未签到
				
				Integer score = member.getScore();
				score = (score==null) ? 1 :score+userDefine.getSignScore();
				member.setScore(score);
				member.setUpdateTime(new Date());
				member.setLastSignTime(DateUtils.getTodayString());
				wechatMemberService.save(member);
				
				reply = WechatUtils.processUserBusinessReply(user.getId(), upcon, score);
				if(com.thinkgem.jeesite.common.utils.StringUtils.isEmpty(reply)){
					reply = "签到成功,您当前的积分为:"+score+"分!";
				}else{
					reply = reply.replaceAll("%分值%", userDefine.getSignScore()+"");
					reply = reply.replaceAll("%积分%", score+"");
				}
				
				ScoreUtils.addWechatMemberSignScore(member,userDefine.getSignScore());
				
				
				List<BusinessNewsReply>	bnrList = businessNewsReplyService.findByTypeSourceId("business", user.getId() , userBusiness.getId()+"");
				
				/**
				 * 多图文回复
				 */
				if(bnrList != null  && !bnrList.isEmpty() ){
//					BusinessNewsReply bnr = new BusinessNewsReply();
//					bnr.setDescription(reply);
//					bnr.setTitle("签到");
//					bnr.setPicUrl(picUrl);
//					bnr.setUrl(url);
//					businessNewsList.add(0, bnr);
					
					List<NewsReply> newsReplies = DataUtils.convertToNewsReplyList(bnrList);
					if(newsReplies!=null  && !newsReplies.isEmpty()){
						for(NewsReply newsReply : newsReplies){
							String desc = newsReply.getDescription();
							desc = desc.replaceAll("%分值%", userDefine.getSignScore()+"");
							desc = desc.replaceAll("%积分%", score+"");
							newsReply.setDescription(desc);
						}
						
						
						this.writeMultiImagesToUser(newsReplies);
						return null;
					}
					
				}
				
			}
			
			
			//回复内容：
			writeToUser(reply);
			
		}
			
		return null;
	}


	@Override
	public boolean isNeedBeMember() {
		return true;
	}

}
